package com.example.transactionhintassistant.controller;

import com.example.transactionhintassistant.pojo.dto.auth.*;
import com.example.transactionhintassistant.pojo.vo.LoginVo;
import com.example.transactionhintassistant.service.AuthService;
import com.example.transactionhintassistant.utils.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
@Tag(name = "1.认证模块")
public class AuthController {

    @Autowired
    private AuthService authService;

    @PostMapping("/register/pwd")
    @Operation(summary = "用户注册")
    public Result<LoginVo> userRegister(@RequestBody @Validated RegisterDto registerDto) {
        return authService.userRegister(registerDto);
    }

    @PostMapping("/login/pwd")
    @Operation(summary = "用户登录")
    public Result<LoginVo> userLogin(@RequestBody @Validated LoginDto loginDto) {
        return authService.userLogin(loginDto);
    }

    @PostMapping("/login/email")
    @Operation(summary = "用户邮箱登录")
    public Result<LoginVo> userLoginByEmail(@RequestBody @Validated EmailAndCaptchaDto emailAndCaptchaDto) {
        return authService.userLoginByEmail(emailAndCaptchaDto);
    }

    @PutMapping("/change/pwd")
    @Operation(summary = "通过原密码修改密码")
    public Result<Object> changePwd(@RequestBody @Validated ChangePwdDto changePwdDto) {
        return authService.changeUserPassword(changePwdDto);
    }

    @PutMapping("/change/pwd/byEmail")
    @Operation(summary = "通过邮箱修改密码")
    public Result<Object> changeUserPwdByEmail(@RequestBody @Validated ChangePwdByEmailDto changePwdByEmailDto) {
        return authService.changeUserPwdByEmail(changePwdByEmailDto);
    }

    @PostMapping("/verify/oldEmail")
    @Operation(summary = "验证旧邮箱", security = {@SecurityRequirement(name = "Authorization")})
    public Result<Object> verifyOldEmail(@RequestBody @Validated EmailAndCaptchaDto emailAndCaptchaDto){
        return authService.verifyOldEmail(emailAndCaptchaDto);
    }

    @PutMapping("/change/newEmail")
    @Operation(summary = "验证并修改邮箱", security = {@SecurityRequirement(name = "Authorization")})
    public Result<Object> verifyNewEmail(@RequestBody @Validated ChangeEmailDto changeEmailDto){
        return authService.verifyNewEmail(changeEmailDto);
    }

    @PostMapping("/binding/email")
    @Operation(summary = "绑定邮箱", security = {@SecurityRequirement(name = "Authorization")})
    public Result<Object> bindingEmail(@RequestBody @Validated EmailAndCaptchaDto emailAndCaptchaDto){
        return authService.bindingEmail(emailAndCaptchaDto);
    }

}
